package com.berchen.函数式接口;

import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * Java8 内置的四大核心函数式接口：
 *
 * Consumer<T>：消费型接口。
 *      void accept(T t);
 * Supplier<T>：供给型接口。
 *      T get();
 * Function<T,R>：函数型接口。
 *      R apply(T t);
 * Predicate<T>：断言型接口。
 *      boolean test(T t);
 */
public class TestFunctionalInterface {

    @Test
    public void test1(){

        happy(120.2,con-> System.out.println("消费了"+con+"元"));
    }
    public void happy(double money, Consumer<Double> con){
        con.accept(money);
    }

    @Test
    public void test2(){

        List<Integer> list = gongji(3, () -> (int)(Math.random()*100) );
        list.forEach(con->System.out.println(con));

    }
    public List<Integer> gongji(int num,Supplier<Integer> supplier){

        List<Integer> list=new ArrayList<>();
        for(int i=0;i<num;i++) {
            list.add(supplier.get());
        }
        return list;
    }

    @Test
    public void test3(){

        String chenwanhao = tihuan("chenwanhao", str -> str.toUpperCase());
        System.out.println(chenwanhao);
    }
    public String tihuan(String s1, Function<String,String> function){

        return function.apply(s1);
    }

    @Test
    public void test4(){

        boolean duanyan = duanyan("chen", str -> str.contains("c"));
        System.out.println(duanyan);
    }
    public boolean duanyan(String str, Predicate<String> pre){

        return pre.test(str);
    }
}
